package cn.com.inossem.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.widget.EditText;

import cn.com.inossem.activity.R;

/**
 * 报销弹窗
 * Created by Mickey.Li on 2017-9-1.
 */

public class DialogUtil {
	private static DialogUtil instance;
	private static Context mContext;
	private AlertDialog.Builder adb;
	private EditText dialogET;
	private double MAX_VALUE = 9999999;
	private double MIN_VALUE = 0;

	public enum DialogType {
		NUMBER, TEXT, CHOICE, PROMPT
	}

	public static DialogUtil getInstance(Context context) {
		if (null == instance) {
			instance = new DialogUtil();
		}
		mContext = context;
		return instance;
	}

	public void showDialog(String title, int tag, String[] choices, DialogType type, int maxLimit, DialogCallBack callBack) {
		adb = new AlertDialog.Builder(mContext, R.style.AlertDialog);
		adb.setTitle(title);
		switch (type) {
			case TEXT:
				initET();
				initInput(tag, callBack);
				break;
			case NUMBER:
				initET();
				if (maxLimit > 0) {
					dialogET.setInputType(InputType.TYPE_CLASS_NUMBER);
					dialogET.setHint(mContext.getString(R.string.dialog_hour_hint));
					InputFilter[] mInputFilter = new InputFilter[2];
					mInputFilter[0] = new InputFilter.LengthFilter(7);
					mInputFilter[1] = new MyInputFilter(0, maxLimit > 0 ? maxLimit : 9999999);
					dialogET.setFilters(mInputFilter);
				} else {
					dialogET.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
					dialogET.addTextChangedListener(new TextWatcher() {

						@Override
						public void onTextChanged(CharSequence s, int start, int before, int count) {

						}

						@Override
						public void beforeTextChanged(CharSequence s, int start, int count, int after) {

						}

						@Override
						public void afterTextChanged(Editable s) {
							String temp = s.toString();
							int posDot = temp.indexOf(".");
							if (posDot == 0)
								s.insert(0, "0"); //开头输入.自定填充0.
							if (temp.length() - posDot - 1 > 2 && posDot != -1) {//限制输入两位小数
								s.delete(posDot + 3, posDot + 4);
							}
						}
					});
				}
				initInput(tag, callBack);
				break;
			case CHOICE:
				initChoice(tag, choices, callBack);
				break;
			case PROMPT:
				initPrompt(title, callBack);
				break;
		}
		adb.show();
	}

	private void initInput(final int tag, final DialogCallBack callBack) {
		adb.setView(dialogET)
				.setPositiveButton(mContext.getString(R.string.dialog_sure), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String input = dialogET.getText().toString().trim();
						if (TextUtils.isEmpty(input)) {
							ToastUtil.show(mContext, mContext.getString(R.string.dialog_null_notice));
						} else {
							callBack.back(tag, input, which);
						}
					}
				})
				.setNegativeButton(mContext.getString(R.string.dialog_cancle), null);
	}

	private void initChoice(final int tag, final String[] choices, final DialogCallBack callBack) {
		adb.setSingleChoiceItems(choices, -1, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callBack.back(tag, choices[which], which);
				dialog.dismiss();
			}
		});
	}

	private void initPrompt(String content, final DialogCallBack callBack) {
		adb.setTitle(R.string.dialog_title_conmmon).setMessage(content)
				.setPositiveButton(mContext.getString(R.string.dialog_email_sure), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						callBack.back(-1, null, which);
					}
				})
				.setNegativeButton(mContext.getString(R.string.dialog_email_cancle), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						callBack.back(-1, null, which);
					}
				});
	}

	private void initET() {
		dialogET = new EditText(mContext);
		dialogET.setHint(mContext.getString(R.string.dialog_et_hint));
		dialogET.setGravity(Gravity.CENTER);
		dialogET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
		dialogET.setBackground(null);
		dialogET.setPadding(0, Utils.dip2px(mContext, 20), 0, 0);
		dialogET.setWidth(Utils.dip2px(mContext, 300));
		dialogET.setTextSize(Utils.sp2px(mContext, 6));
		dialogET.setFocusable(true);
	}

	public interface DialogCallBack {
		void back(int tag, String content, int which);
	}
}
